<template>
  <div class="container">
    <div class="app-container">
      <el-button
        class="btn-add"
        size="mini"
        type="primary"
        @click="addPermission(0, 1)"
        >添加权限</el-button
      >
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              v-if="row.type === 1"
              size="mini"
              type="text"
              @click="addPermission(row.id, 2)"
              >添加</el-button
            >
            <el-button size="mini" type="text" @click="editPermission(row.id)"
              >编辑</el-button
            >
            <el-popconfirm
              title="这是一段内容确定删除吗？"
              style="margin-left: 10px"
              @onConfirm='delPermission(row.id)'
            >
              <el-button size="mini" type="text" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="titledata"
      :visible.sync="dialogVisible"
      width="50%"
      @close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" placeholder="请输入权限标识" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入权限描述" />
        </el-form-item>
        <el-form-item label="开启" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: center"
      >
        <el-button type="primary" size="mini" @click="addbtn_ok"
          >确 定</el-button
        >
        <el-button @click="close" size="mini">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getPermissionList,
  addPermissionapi,
  editPermissionapi,
  getPermissionDetailapi,
  delPermissionapi
} from "@/api/permission";
import { transListToTreeData } from "@/utils";
export default {
  name: "Permission",
  computed: {
    titledata() {
      return this.form.id ? "编辑权限点" : "添加权限点";
    },
  },
  data() {
    return {
      dialogVisible: false,
      list: [],
      form: {
        name: "",
        code: "",
        description: "",
        enVisible: "0", //0关闭, 1开启. (暂时无需判断, 前端逻辑上默认全都有效直接用)
        pid: "",
        type: "",
      },
      rules: {
        name: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限标识", trigger: "blur" }],
        description: [
          { required: true, message: "请输入权限描述", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      this.list = transListToTreeData(await getPermissionList(), 0); // 将列表数据转化成树形结构
      console.log(this.list);
    },
    addbtn_ok() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            
            editPermissionapi(this.form).then(() => {
              this.dialogVisible = false;
              this.close();
              this.$message.success("编辑权限成功");
              this.getPermissionList();
            });
          } else {
            addPermissionapi(this.form).then(() => {
              this.dialogVisible = false;
              this.close();
              this.$message.success("添加权限成功");
              this.getPermissionList();
            });
          }
        }
      });
    },
    addPermission(pid, type) {
      this.form.pid = pid;
      this.form.type = type;
      this.dialogVisible = true;
    },
    close() {
      console.log("close");
      this.form = {
        name: "",
        code: "",
        description: "",
        enVisible: "0", //0关闭, 1开启. (暂时无需判断, 前端逻辑上默认全都有效直接用)
        pid: "",
        type: "",
      };
      this.dialogVisible = false;
    },
    async editPermission(id) {
      console.log("start");
      this.form = await getPermissionDetailapi(id);
      this.dialogVisible = true;
    },
    async delPermission(id) {
      await delPermissionapi(id);
      this.$message.success("删除权限成功");
      this.getPermissionList();
    },
  },
};
</script>
<style>
.btn-add {
  margin: 10px;
}
</style>